﻿var salephone = {
    isWait:false,
    init: function() {
        var that = this;
        that.initValidate();
        that.initSendCheckCode();
        that.initPhone();
    },

    doSubmit: function() {
        var that = this;
        $("#modify-pwd").validate({
            rules: {
                phone: 'required phone',
                sendsms: 'required',
            },
            messages: {
                phone: '请输入手机号',
                sendsms: '请输入验证码'
            },
            submitHandler: function(form) {
                // alert('submitHandler');
                $.ajax({
                    url: COMMONURL + "api/Account/EditEmailAndPhone",
                    type: "post",
                    dataType: "json",
                    data: {
                        account: $.cookie('account'),
                        phone: $('[name=oldpwd]').val(),
                        sendsms: $('[name=newpwd]').val(),
                    }
                }).done(function(data) {
                    if (data.recode == 0000) {
                        alert("修改密码成功");
                        that.clear();
                    } else {
                        alert(data.resmessage);
                    }
                })
            }
        })
    },

    initValidate: function() {
        var that = this;
        // 添加重复密码的规则
        jQuery.validator.addMethod("phone", function(value, element) {
            if (that.checkPhone(value))
                return true;
            else
                return false;
        }, '请输入正确的手机号码');
        // 给表单添加验证
        $("#phone-bind").validate({
            rules: {
                phone: 'required',
                checkcode: 'required',
            },
            messages: {
                phone: '请输入手机号',
                checkcode: '请输入验证码',
            }
        })
    },

    clear: function() {
        $('#phone').val('');
        $('#sendsms').val('');
    },
    // 发送验证码
    initSendCheckCode: function() {
        var that = this;
        $('#sendsms').on('click', function() {
            if(that.isWait){
                return;
            }
            var phone = $('#phone').val();
            if (!that.checkPhone(phone)) {
                alert('请输入正确的手机号码');
                return;
            }
            that.wait();
            $.ajax({
                url: "http://api.wl.7yunbao.com/Logistics/api/Account/sendYzmToPhone",
                type: "post",
                dataType: "json",
                data: {
                    token: $.cookie('token'),
                    channel: 200,
                    version: 1,
                    phone: phone
                }
            }).done(function(data) {
                if (data.code == 0000) {
                    alert("验证码发送成功");
                    
                } else {
                    alert(data.message);
                }
            })
        });

    },
    // 验证手机号码
    checkPhone: function(phone) {
        return /^1[34589]\d{9}$/.test(phone);
    },
    // 验证码发送成功之后等待60s
    wait: function() {
        var that = this;
        var btn = $('#sendsms').get(0);
        var wait = 60;
        that.isWait = true;
        time(btn);

        function time(o) {
            if (wait == 0) {
                o.removeAttribute("disabled");
                o.innerHTML = "发送验证码";
                wait = 60;
                that.isWait = false;
            } else {
                o.setAttribute("disabled", true);
                o.innerHTML = "重新发送(" + wait + ")";
                wait--;
                setTimeout(function() {
                        time(o)
                    },
                    1000)
            }
        }

    },
    initPhone: function() {
        $.ajax({
            url: COMMONURL + "api/Account/GetAccountInfo?token="+$.cookie('token'),
            type: "post",
            dataType: "json",
            data: {
                account: $.cookie('account')
            }
        }).done(function(recode) {
            if (recode.recode == 0000) {
                $("#phone").val(recode.mod.phone);
            }
        }) 
    }

}

salephone.init();
